﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using ServerApi.Controllers;
using ServerApi.Models;
using TuyennvLibrary.Utl;
using TuyennvLibrary.ServerApi.Models;
namespace ServerApi.administrator
{
    public partial class PartnerSMSManager : System.Web.UI.Page
    {
        public int stt = 1;
        public string title = "Thêm đối tác mới";
        PartnerSMSModel partnerModel = new PartnerSMSModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int id = int.Parse(Request["id"]);
                    loadinfo(id);
                    title = "Cập nhật đối tác";
                }
                Search();
            }
        }
        private void loadinfo(int id)
        {
            PartnerSMS obj = partnerModel.GetByID(id);
            loadinfo(obj);
        }
        private void loadinfo(PartnerSMS obj)
        {
            txtName.Text = obj.name;
            txtServiceid.Text = obj.serviceid;
            chkStatus.Checked = (obj.status == 1) ? true : false;
            txtParamUserid.Text = obj.param_userid;
            txtParamServiceid.Text = obj.param_serviceid;
            txtParamMessage.Text = obj.param_message;
            txtParamCommand.Text = obj.param_commandcode;
            txtListIP.Text = obj.ip_access;
            txtUsername.Text = obj.username;
            txtPassword.Text = obj.password;
            txtClassProccessScript.Text = obj.class_proccess_script;
        }
        public void Search()
        {
            DataTable dt = partnerModel.Search(txtNameSearch.Text,txtServiceidSearch.Text,int.Parse(ddlStatus.Text));
            GrvListPartner.DataSource = dt;
            GrvListPartner.DataBind();
        }
        protected void LbtnAccept_Click(object sender, EventArgs e)
        {
            PartnerSMS obj = new PartnerSMS();
            obj.name = txtName.Text;
            obj.name_unsign = obj.name.GetUnsignText();
            obj.serviceid = txtServiceid.Text;
            obj.status = (chkStatus.Checked) ? 1 : 0;
            obj.param_commandcode = txtParamCommand.Text;
            obj.param_message = txtParamMessage.Text;
            obj.param_serviceid = txtParamServiceid.Text;
            obj.param_userid = txtParamUserid.Text;
            obj.ip_access = Regex.Replace(txtListIP.Text,"\\s+","");
            obj.username = txtUsername.Text;
            obj.password = txtPassword.Text;
            obj.class_proccess_script = txtClassProccessScript.Text;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                obj.id = int.Parse(Request["id"]);
                if (partnerModel.Update(obj))
                {
                    Utilities.Alert(Page, this.GetType(), "Đã cập nhật đối tác thành công");
                    Response.Redirect("PartnerSMSManager.aspx");
                }
            }
            else
            {
                if (partnerModel.Insert(obj))
                {
                    Utilities.Alert(Page, this.GetType(), "Đã thêm mới đối tác thành công");
                }
            }
            Search();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }
    }
}